//
//  ForgetPasswordFinishViewController.m
//  HuLaQuan
//
//  Created by hok on 1/14/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "ForgetPasswordFinishViewController.h"

#import "YuLoginInputInfoField.h"

#import "LoginRelatedHeader.h"

@interface ForgetPasswordFinishViewController()<UIGestureRecognizerDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@property (weak, nonatomic) IBOutlet YuLoginInputInfoField *passwordField;

@property (weak, nonatomic) IBOutlet YuLoginInputInfoField *retypePasswordField;



@end
@implementation ForgetPasswordFinishViewController

- (void)setupUI
{
    self.title = @"忘记密码";
    
    [self setupNavBackButton];
    
    [self setupTextField];
    
    // 加手势处理键盘
    [self setupTapGesture];
}

// 加手势处理键盘
- (void)setupTapGesture
{
    UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelGenericTapView:)];
    tapRec.delegate = self;
    [self.view addGestureRecognizer:tapRec];
}
- (void)handelGenericTapView:(UITapGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        // handling code
        // [self.view endEditing:YES];
        [[UIApplication sharedApplication].keyWindow endEditing:YES];
    }
}


- (void)setupTextField
{
    //    self.nameField.imageName = @"name.png";
    //    self.secretField.imageName = @"password.png";
    [self.passwordField setColorfulPlaceholder:kInputNewPasswordPlaceholder];
    [self.retypePasswordField setColorfulPlaceholder:kConfirmNewPasswordPlaceholder];
}
- (IBAction)tapConfirmButton:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}


#pragma mark - Textfield delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    YuLoginInputInfoField *textF = (YuLoginInputInfoField *)textField;
    [textF setColorfulPlaceholder:@""];
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length == 0 ) {
        YuLoginInputInfoField *textF = (YuLoginInputInfoField *)textField;
        [textF setColorfulPlaceholder:([textField isEqual:self.passwordField] ? kInputNewPasswordPlaceholder : kConfirmNewPasswordPlaceholder)];
        
    }
    if (self.passwordField.text.length == 0 || self.retypePasswordField.text.length == 0) {
        self.confirmButton.enabled = NO;
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (self.passwordField.text.length > 0 && self.retypePasswordField.text.length > 0) {
        self.confirmButton.enabled = YES;
    }
    //检测密码长度
    ////NSLog(@"%@",string);
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (self.passwordField == textField &&self.retypePasswordField == textField)
    {
        if ([toBeString length] > kPasswordLength) {
            textField.text = [toBeString substringToIndex:kPasswordLength];
            return NO;
        }
    }
    
    return YES;
    // return [self validateNumber:string];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
